# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

class Ribs_3_anti():

  def __init__(self, H, D, X_N, Y_N):

    self.H = H         # 胞元间连杆长度
    self.D = D         # 圆直径
    self.R = 0.5 * D   # 圆半径

    # 大小规格（阵列数目）
    self.X_N = X_N      
    self.Y_N = Y_N

    # 阵列间距
    self.X_SPACE = H
    self.Y_SPACE = 3 ** 0.5 * H

  def sin(self, theta):
    return math.sin(theta / 180.0 * math.pi)

  def cos(self, theta):
    return math.cos(theta / 180.0 * math.pi)

  # 一点绕某一点旋转一定角度后的坐标
  def rotatePoint(self, point, center, angle):
    x = (point[0] - center[0]) * self.cos(angle) - (point[1] - center[1]) * self.sin(angle) + center[0]
    y = (point[0] - center[0]) * self.sin(angle) + (point[1] - center[1]) * self.cos(angle) + center[1]
    return (x, y)

  # 在坐标(x, y)点创建手性或反手性胞元
  def createCellUnit(self, x, y, isAnti):

    H = self.H
    R = self.R

    # 绘制圆
    mdb.models['Model-1'].sketches['__profile__'].CircleByCenterPerimeter(center=(x, y), point1=(x+R, y))

    center = (x, y)

    # 第一条切线坐标
    if(isAnti == False):
      point_1_1 = (-R + x, y)
      point_1_2 = (-R + x, -0.5 * H + y)
    else:
      point_1_1 = (-R + x, y)
      point_1_2 = (-R + x, 0.5 * H + y)
    
    # 通过旋转得到其他五条切线坐标
    point_2_1 = self.rotatePoint(point_1_1, center, 120)
    point_2_2 = self.rotatePoint(point_1_2, center, 120)

    point_3_1 = self.rotatePoint(point_1_1, center, 240)
    point_3_2 = self.rotatePoint(point_1_2, center, 240)
    
    # 绘制六条切线
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_1_1, point2=point_1_2)
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_2_1, point2=point_2_2)
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_3_1, point2=point_3_2)

  # 按规律排列胞元
  def arrayCellUnit(self, start_x, start_y, x_n, x_space, y_n, y_space, isAnti):

    for i in range(y_n):

      for j in range(x_n):

        x = j * x_space + start_x
        y = i * y_space + start_y
        
        self.createCellUnit(x, y, isAnti)

  # 打印参数
  def printParameters(self):

    print(" ")
    print("H: %s " % self.H)
    print("D: %s " % self.D)
    print("X_N: %d " % self.X_N)
    print("Y_N: %d " % self.Y_N)

  def part(self):

    H = self.H
    X_N = self.X_N
    Y_N = self.Y_N

    # 创建草图
    mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)

    self.arrayCellUnit(0.0, 0.0, X_N, 3**0.5*H, Y_N, 3*H, False)
    self.arrayCellUnit(-0.5*3**0.5*H, 0.5*H, X_N+1, 3**0.5*H, Y_N, 3*H, True)
    self.arrayCellUnit(-0.5*3**0.5*H, 1.5*H, X_N+1, 3**0.5*H, Y_N, 3*H, False)
    self.arrayCellUnit(0.0, 2*H, X_N, 3**0.5*H, Y_N, 3*H, True)

    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=
        DEFORMABLE_BODY)
    mdb.models['Model-1'].parts['Part-1'].BaseWire(sketch=
        mdb.models['Model-1'].sketches['__profile__'])
    del mdb.models['Model-1'].sketches['__profile__']

def main(H, D, X_N, Y_N):
  ribs_3_anti = Ribs_3_anti(H, D, X_N, Y_N)
  ribs_3_anti.part()

if __name__ == '__main__':

  main(H=10.0, D=4.0, X_N=5, Y_N=3)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
